﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
namespace PasteLoggerBoard.usermodels
{
    /// <summary>
    /// 
    ///</summary>
    [TypeFilter(typeof(RoleAttribute), Arguments = new object[] { "root", "root" })]
    public class WebHookLogAppService : PasteLoggerBoardAppService
    {

        private readonly IPasteLoggerBoardDbContext _dbContext;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbContext"></param>
        public WebHookLogAppService(IPasteLoggerBoardDbContext dbContext)
        {
            _dbContext = dbContext;
        }
        /// <summary>
        /// 按页获取
        ///</summary>
        /// <param name="page">页码</param>
        /// <param name="size">页大小</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<PagedResultDto<WebHookLogListDto>> GetListAsync(int page = 1, int size = 20)
        {

            var query = _dbContext.WebHookLog.Where(t => 1 == 1).OrderByDescending(xy => xy.Id);
            var pagedResultDto = new PagedResultDto<WebHookLogListDto>();
            pagedResultDto.TotalCount = await query.CountAsync();
            var userList = await query.Page(page, size).ToListAsync();
            var temList = ObjectMapper.Map<List<WebHookLog>, List<WebHookLogListDto>>(userList);
            pagedResultDto.Items = temList;
            return pagedResultDto;
        }

        /// <summary>
        /// 根据ID获取单项
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public WebHookLogDto GetByIdAsync(long id)
        {
            var query = _dbContext.WebHookLog.Where(t => t.Id == id)
            .AsNoTracking().FirstOrDefault();
            if (query == null || query == default)
            {
                throw new PasteLoggerBoardException("没有找到这样的信息", 404);
            }
            var temList = ObjectMapper.Map<WebHookLog, WebHookLogDto>(query);
            return temList;
        }

        /// <summary>
        /// 根据ID获取待更新单项信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public WebHookLogUpdateDto GetInfoForUpdateAsync(long id)
        {
            var query = _dbContext.WebHookLog.Where(t => t.Id == id)
            .AsNoTracking().FirstOrDefault();
            if (query == null || query == default)
            {
                throw new PasteLoggerBoardException("没有找到这样的信息", 404);
            }
            var temList = ObjectMapper.Map<WebHookLog, WebHookLogUpdateDto>(query);
            return temList;
        }


        /// <summary>
        /// 添加一个
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebHookLogDto> CreateItemAsync(WebHookLogAddDto input)
        {

            var newu = ObjectMapper.Map<WebHookLogAddDto, WebHookLog>(input);
            newu.CreateDate = DateTime.Now;            //添加自定义
            _dbContext.Add(newu);
            await _dbContext.SaveChangesAsync();
            //var updated = await _repository.InsertAsync(newu,true);
            var backinfo = ObjectMapper.Map<WebHookLog, WebHookLogDto>(newu);
            return backinfo;
        }
        /// <summary>
        /// 更新一个
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebHookLogDto> UpdateItemAsync(WebHookLogUpdateDto input)
        {
            var info = await _dbContext.WebHookLog.Where(x => x.Id == input.Id)
.FirstOrDefaultAsync();
            if (info == null || info == default)
            {
                throw new UserFriendlyException("需要查询的信息不存在", "404");
            }
            ObjectMapper.Map<WebHookLogUpdateDto, WebHookLog>(input, info);
            //var updated = await _repository.UpdateAsync(newu);
            await _dbContext.SaveChangesAsync();
            var backinfo = ObjectMapper.Map<WebHookLog, WebHookLogDto>(info);
            return backinfo;
        }
    }
}
